package com.ewanghuiju.app.ui.pintuan.activity;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RootActivity;
import com.ewanghuiju.app.base.contract.pintuan.PintuanPingjiaContract;
import com.ewanghuiju.app.model.bean.response.PingjiaListResponBean;
import com.ewanghuiju.app.presenter.pintuan.PintuanPingjiaPresenter;
import com.ewanghuiju.app.ui.pintuan.adapter.PintuanPingjiaAdapter;
import com.ewanghuiju.app.util.GlideSimpleLoader;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * @author sj  qq:751943509
 * @class 店铺页面
 * @time 2020/2/19
 */
public class PintuanPingjiaActivity extends RootActivity<PintuanPingjiaPresenter> implements PintuanPingjiaContract.View {

    //    @BindView(R.id.flowLayout_select)
//    TagFlowLayout flowLayoutSelect;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

//    private GlideSimpleLoader glideSimpleLoader;

    private int shopId;

    private int currentPage = 1;

    private PintuanPingjiaAdapter pintuanPingjiaAdapter;


    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_pintuan_pingjia;
    }

    @Override
    protected void initEventAndData() {

        shopId = getIntent().getIntExtra(Constants.PINTUAN_SHOP_ID, -1);

//        glideSimpleLoader = new GlideSimpleLoader();
//        glideSimpleLoader.onCreate(mContext);

        refreshLayout.setEnableRefresh(true);//是否启用下拉刷新功能
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage++;
                mPresenter.getPjList(currentPage, shopId + "");
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage = 1;
                refreshLayout.resetNoMoreData();
                mPresenter.getPjList(currentPage, shopId + "");
            }
        });
        intPingjiaList();
        //首次进入显示
        LoadingDialogUtils.show(mContext);
        currentPage = 1;
        mPresenter.getPjList(currentPage, shopId + "");

    }


    @Override
    protected int getTitleIntText() {
        return R.string.all_pingjia_title;
    }

    /**
     * tag
     */
//    private void initFlowView() {
//        List<String> titleList = new ArrayList<>();
//        titleList.add("全部(13246)");
//        titleList.add("最新");
//        titleList.add("质量很好");
//        titleList.add("宽松");
//        flowLayoutSelect.setAdapter(new TagAdapter(titleList) {
//            @Override
//            public View getView(FlowLayout parent, int position, Object o) {
//                TextView tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_pingjia_tag_flowlayout,
//                        flowLayoutSelect, false);
//                tv.setText((String) o);
//                return tv;
//            }
//        });
//    }
    @Override
    protected void intSmartRefreshLayout() {
    }

    /**
     * 评价列表
     */
    private void intPingjiaList() {
        pintuanPingjiaAdapter = new PintuanPingjiaAdapter(R.layout.item_pintuan_pingjia);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(pintuanPingjiaAdapter);
        recyclerView.setNestedScrollingEnabled(false);
//        recyclerView.setFocusable(false);
//        pintuanPingjiaAdapter.setNewData(pingjiaListResponBeans);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

//    @Override
//    public void onBackPressedSupport() {
//        if (glideSimpleLoader != null && !glideSimpleLoader.onBackPressed()) {
//            super.onBackPressed();
//        }
//    }

    @Override
    public void showContent(List<PingjiaListResponBean> list) {
        LoadingDialogUtils.dismissDialog_ios();
        try {
            if (list == null) {
                list = new ArrayList<>();
            }
            if (currentPage == 1) {
                refreshLayout.finishRefresh();
                pintuanPingjiaAdapter.setNewData(list);
            } else {
                if (list.size() < Constants.NUM_OF_PAGE) {
                    refreshLayout.finishLoadMoreWithNoMoreData();
                } else {
                    refreshLayout.finishLoadMore();
                }
                pintuanPingjiaAdapter.addData(list);
            }

            if (pintuanPingjiaAdapter.getData().size() > 0) {
                stateMain();
            } else {
                stateEmpty();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showContentError() {
        LoadingDialogUtils.dismissDialog_ios();
        if (currentPage == 1) {
            refreshLayout.finishRefresh();
        } else {
            currentPage--;
            refreshLayout.finishLoadMore();
        }
        if (pintuanPingjiaAdapter.getData().size() == 0) {
            super.stateError();
        }
    }
}
